import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@WebServlet(name = "kindServlet")
public class kindServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        int id = Integer.parseInt(request.getParameter("id"));
        String []names= {"ad钙奶","泡面","香肠","雀巢"};
        String name=names[id];

        Map<String,Integer> map=(Map<String, Integer>) request.getSession().getAttribute("kind");
        if(map==null) {
            map=new HashMap<String, Integer>();
            request.getSession().setAttribute("kind", map);
        }
        if(map.containsKey(name)) {
            map.put(name,map.get(name)+1);
        }else {
            map.put(name,1);
        }
        response.getWriter().write("<a href='./web/add.jsp'><h1>继续购买</h1></a>");
        response.getWriter().write("<a href='./web/show.jsp'><h1>去购物车结算</h1></a>");
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}